using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Rotempco.Core.Logging.Objects;
using Rotempco.Core.Logging.EntityObjects.Objects;

namespace Rotempco.Core.Logging.Logic
{
    internal class Log
    {
        internal delegate void LogDelegate(LogStack logEntry);
        internal static event LogDelegate LogEvent;

        internal static void LogNow(string message, Exception exception, int extraSteps)
        {
            string exceptionString = string.Empty;

            if (exception != null)
                exceptionString = "Exception: " + exception.Message + Environment.NewLine + "Stacktrace: " + exception.StackTrace;

            StackFrame stackFrame = new StackTrace(true).GetFrame(1 + extraSteps);

            LogStack logEntry = new LogStack()
            {
                TimeStamp = DateTime.Now,
                Message = message,
                Exception = exceptionString,
                SourceMachineName = Environment.MachineName,
                Method = stackFrame.GetMethod().Name,
                ClassName = stackFrame.GetMethod().DeclaringType.FullName
            };

            if (LogEvent != null)
                LogEvent(logEntry);

            Logger.Instance.AddToLogQueue(logEntry);
        }

    }
}
